package com.office.shop.by;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import com.office.shop.by.components.CAccounts;

public class SettingsActivity extends Activity {

    private ListView lv1;
    private String login = "";
    private String pwd = "";

    @Override
    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
        if(AccountsActivity.obj.getId() != -1)
        {
           login = CAccounts.logins[AccountsActivity.obj.getId()];
           pwd = CAccounts.passwords[AccountsActivity.obj.getId()];
        }

         String lv_arr[]={
                             getString(R.string.Login),
                             getString(R.string.Password)
                            };
        lv1 = (ListView)findViewById(R.id.ListView01);
        lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
        lv1.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                switch (i) {
                    case 0:
                        SettingsActivity.this.CustomDialog(getString(R.string.Login),0);
                        break;
                    case 1:
                        SettingsActivity.this.CustomDialog(getString(R.string.Password),1);
                        break;
                }
            }
        });
    }

    public void CustomDialog(String title,final int type)
    {
        final AlertDialog.Builder alert = new AlertDialog.Builder(this);
        final EditText input = new EditText(this);

        alert.setTitle(title);
        alert.setView(input);

        switch(type)
        {
            case 0:
            input.setText(this.login);
            break;
            case 1:
            input.setText(this.pwd);
            break;
        }
        alert.setPositiveButton(getString(R.string.Save), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                String value = input.getText().toString().trim();
                switch(type)
                {
                        case 0:
                        SettingsActivity.this.login = value;
                        break;
                        case 1:
                        SettingsActivity.this.pwd = value;
                        break;
                }
                dialog.cancel();
            }
        });
        alert.setNegativeButton(getString(R.string.Cancel), new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int whichButton) {
                          dialog.cancel();
                      }
                  });

        alert.show();
    }

    public void onBackPressed()
    {
        AccountsActivity.obj.addAccount(this.login,this.pwd);
        finish();
    }


}